﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace HML
{
    /*使用例子*/
    /*
        
        private ImageIndexer 例子imageIndexer = new ImageIndexer();
        private ImageList 例子imageList;
        /// <summary>
        /// ImageList
        /// </summary>
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public ImageList 例子ImageList
        {
            get { return this.例子imageList; }
            set
            {
                if (例子imageList == value)
                    return;

                EventHandler recreateHandler = new EventHandler(this.RecreateImageListHandler);
                EventHandler disposedHandler = new EventHandler(this.DetachImageListHandler);

                if (this.例子imageList != null)
                {
                    this.例子imageList.RecreateHandle -= recreateHandler;
                    this.例子imageList.Disposed -= disposedHandler;
                }

                this.例子imageList = value;
                this.例子imageIndexer.SyncIndexer(value);//同步索引管理器

                if (value != null)
                {
                    this.例子imageList.RecreateHandle += recreateHandler;
                    this.例子imageList.Disposed += disposedHandler;
                }
                this.Invalidate();
            }
        }

        /// <summary>
        /// 图片
        /// </summary>
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.Repaint)]
        public Image 例子图片
        {
            get { return this.例子imageIndexer.Image; }
            set
            {
                if (!this.例子imageIndexer.EqualImage(value))
                {
                    this.例子imageIndexer.Image = value;
                    this.Invalidate();
                }
            }
        }

        /// <summary>
        /// 图片Index 
        /// </summary>
        [DefaultValue(-1)]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [TypeConverter(typeof(ImageIndexConverter))]
        public int 例子Index
        {
            get { return this.例子imageIndexer.Index; }
            set
            {
                if (!this.例子imageIndexer.EqualIndex(value))
                {
                    this.例子imageIndexer.Index = value;
                    this.Invalidate();
                }
            }
        }

        /// <summary>
        /// 图片Key 
        /// </summary>
        [DefaultValue("")]
        [Localizable(true)]
        [RefreshProperties(RefreshProperties.Repaint)]
        [TypeConverter(typeof(ImageKeyConverter))]
        public string 例子Key
        {
            get { return this.例子imageIndexer.Key; }
            set
            {
                if (!this.例子imageIndexer.EqualKey(value))
                {
                    this.例子imageIndexer.Key = value;
                    this.Invalidate();
                }
            }
        }

        /// <summary>
        /// 控件释放
        /// </summary>
        /// <param name="disposing"></param>
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.例子imageList != null)
                {
                    this.例子imageList.RecreateHandle -= new EventHandler(this.RecreateImageListHandler);
                    this.例子imageList.Disposed -= new EventHandler(this.DetachImageListHandler);
                    this.例子imageList = null;
                }
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// ImageList更改后刷新控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void RecreateImageListHandler(object sender, EventArgs e)
        {
            if (this.IsHandleCreated)
            {
                this.Invalidate();
            }
        }

        /// <summary>
        /// ImageList释放时解绑清除清除引用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DetachImageListHandler(object sender, EventArgs e)
        {
            this.例子imageList = null;
        }



     */

    /// <summary>
    /// ImageList图片集合Index、Key管理器
    /// </summary>
    public class ImageIndexer
    {
        #region 属性

        private ImageList imageList;
        /// <summary>
        /// 要管理的图片集合
        /// </summary>
        public ImageList ImageList
        {
            get { return this.imageList; }
            set { this.imageList = value; }
        }

        private Image image = null;
        /// <summary>
        /// 存放独立设置图片 
        /// get: 返回优先级 独立设置Image>当Image==null根据最后一次赋值时是Index还是Key，初始默认为Index；
        /// set: 赋值后，当value!=null时index=-1、key=string.Empty；
        /// </summary>
        public Image Image
        {
            get
            {
                if (this.image != null)
                {
                    return this.image;
                }

                if (this.imageList != null)
                {
                    int actualIndex = this.ActualIndex;

                    if (-1 < actualIndex && actualIndex < this.imageList.Images.Count)
                    {
                        return this.imageList.Images[actualIndex];
                    }
                    return null;
                }
                return null;
            }
            set
            {
                if (this.image == value)
                    return;

                this.image = value;
                if (this.image != null)
                {
                    this.index = -1;
                    this.key = string.Empty;
                }
            }
        }

        private string key = string.Empty;
        /// <summary>
        /// 图片Key
        /// get: 返回图片Key，没有设置的返回"" ；  
        /// set: 赋值后，当value!=(null||string.Empty)时index=-1、image=null；
        /// </summary>
        public string Key
        {
            get { return this.key; }
            set
            {
                if (value == null)
                {
                    value = string.Empty;
                }
                else
                {
                    value = value.Trim();
                }

                if (this.key == value)
                    return;

                if (value != string.Empty)
                {
                    this.image = null;
                    this.index = -1;
                    this.useIntegerIndex = false;
                }
                this.key = value;
            }
        }

        private int index = -1;
        /// <summary>
        /// 图片索引
        /// get: 返回图片索引，没有设置的返回-1；   
        /// set: 赋值后，value!=-1时key=string.Empty、image=null；
        /// </summary>
        public int Index
        {
            get { return this.index; }
            set
            {
                if (this.index == value || value < -1)
                    return;

                if (value != -1)
                {
                    this.image = null;
                    this.key = string.Empty;
                    this.useIntegerIndex = true;
                }
                this.index = value;
            }
        }

        #endregion

        #region 字段

        /// <summary>
        /// 获取ActualIndex属性值时是否应该根据Index属性返回索引，否则就根据Key从ImageList中返回索引
        /// </summary>
        private bool useIntegerIndex = true;

        #endregion

        #region 公开方法

        /// <summary>
        /// ImageList修改后应同步ImageIndexer,当value!=null时image = null；
        /// </summary>
        /// <param name="value">ImageList</param>
        public void SyncIndexer(ImageList value)
        {
            if (this.imageList == value)
                return;
            
            this.imageList = value;
            if (value != null)
            {
                this.image = null;
            }
        }

        /// <summary>
        /// 判断独立设置Image是否一样
        /// </summary>
        /// <param name="image">要判断独立设置Image</param>
        /// <returns></returns>
        public bool EqualImage(Image image)
        {
            return this.image == image;
        }

        /// <summary>
        /// 判断Key是否一样
        /// </summary>
        /// <param name="key">要判断Key</param>
        /// <returns></returns>
        public bool EqualKey(string key)
        {
            if (key == null)
            {
                key = string.Empty;
            }
            else
            {
                key = key.Trim();
            }

            return this.key == key;
        }

        /// <summary>
        /// 判断Index是否一样
        /// </summary>
        /// <param name="index">要判断Index</param>
        /// <returns></returns>
        public bool EqualIndex(int index)
        {
            return this.index == index;
        }

        #endregion

        #region 私有方法

        /// <summary>
        /// 获取图片索引，没有设置的返回-1（根据最后一次赋值时是Index还是Key，初始默认为Index）
        /// </summary>
        private int ActualIndex
        {
            get
            {
                if (this.useIntegerIndex)
                {
                    return this.Index;
                }
                if (this.ImageList != null)
                {
                    return this.ImageList.Images.IndexOfKey(this.Key);
                }
                return -1;
            }
        }

        #endregion

    }

}
